package com.ruoyi.qywx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.entity.ExtFollowContact;
import com.ruoyi.qywx.getway.api.ContactTagApi;
import com.ruoyi.qywx.getway.entity.QyExtContactRemark;
import com.ruoyi.qywx.mapper.ExtFollowContactMapper;
import com.ruoyi.qywx.model.vo.CustomerFollowerVo;
import com.ruoyi.qywx.service.inters.ExtFollowContactService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author : hcl
 * @Date : 2023/8/22 16:06
 */
@Service
public class ExtFollowContactServiceImpl implements ExtFollowContactService {
    @Resource
    private ExtFollowContactMapper extFollowerMapper;

    @Transactional
    @Override
    public void insertFollower(ExtFollowContact follower) {
        if (Objects.isNull(follower)) return;
        ExtFollowContact old = extFollowerMapper.selectOne(new QueryWrapper<ExtFollowContact>()
                .eq("ext_user_id", follower.getExtUserId())
                .eq("user_id", follower.getUserId())
        );
        if (Objects.isNull(old)) {
            extFollowerMapper.insert(follower);
        } else {
            UpdateWrapper<ExtFollowContact> update = new UpdateWrapper<>();
            if (!StrUtils.equals(old.getRemark(), follower.getRemark())) {
                update.set("remark", follower.getRemark());
            }
            if (!StrUtils.equals(old.getDescription(), follower.getDescription())) {
                update.set("description", follower.getDescription());
            }
            if (!StrUtils.equals(old.getRemarkCorpName(), follower.getRemarkCorpName())) {
                update.set("remark_corp_name", follower.getRemarkCorpName());
            }
            if (!StrUtils.equals(old.getRemarkMobiles(), follower.getRemarkMobiles())) {
                update.set("remark_mobiles", follower.getRemarkMobiles());
            }
            if (!old.getAddWay().equals(follower.getAddWay())) {
                update.set("add_way", follower.getAddWay());
            }
            update.set("is_delete", false)
                    .set("del_contact", false)
                    .set("del_follower", false)
                    .set("add_time", follower.getAddTime())
                    .eq("id", old.getId());
            extFollowerMapper.update(follower, update);
        }
    }

    @Override
    public boolean updateExtUserRemark(QyExtContactRemark remark, Meta meta, boolean updateTab) {
        boolean success = ContactTagApi.editExternalContactRemark(meta, remark);
        if (success && updateTab) {
            updateFollowerRemark(remark);
        }
        return success;
    }

    @Transactional
    @Override
    public void updateFollowerRemark(QyExtContactRemark remark) {
        if (StrUtils.isEmpty(remark.getExternal_userid()) || StrUtils.isEmpty(remark.getUserid())) {
            return;
        }
        UpdateWrapper<ExtFollowContact> update = new UpdateWrapper<>();
        if (StrUtils.isNotEmpty(remark.getRemark())) {
            update.set("remark", remark.getRemark());
        }
        if (Objects.nonNull(remark.getRemark_company())) {
            update.set("remark_corp_name", remark.getRemark_company());
        }
        if (Objects.nonNull(remark.getDescription())) {
            update.set("description", remark.getDescription());
        }
        if (CollectionUtils.isNotEmpty(remark.getRemark_mobiles())) {
            update.set("remark_mobiles", CollectionUtils.join(remark.getRemark_mobiles(), StrUtils.COMMA));
        }
        if (StrUtils.isNotEmpty(update.getSqlSet())) {
            extFollowerMapper.update(null, update
                    .eq("ext_user_id", remark.getExternal_userid())
                    .eq("user_id", remark.getUserid())
            );
        }
    }

    @Transactional
    @Override
    public void deleteExternalContact(String externalUserId, String userId) {
        extFollowerMapper.update(null, new UpdateWrapper<ExtFollowContact>()
                .set("is_delete", true)
                .set("del_contact", true)
                .eq("ext_user_id", externalUserId)
                .eq("user_id", userId)
        );
    }

    @Transactional
    @Override
    public void deleteFollower(String externalUserId, String userId) {
        extFollowerMapper.update(null, new UpdateWrapper<ExtFollowContact>()
                .set("is_delete", true)
                .set("del_follower", true)
                .set("delete_time", new Date())
                .eq("ext_user_id", externalUserId)
                .eq("user_id", userId)
        );
    }

    @Override
    public boolean isAllLost(String extUserId) {
        Long noLostCount = extFollowerMapper.selectCount(new QueryWrapper<ExtFollowContact>()
                .eq("ext_user_id", extUserId)
                .eq("is_delete", false)
        );
        return noLostCount == 0;
    }

    @Override
    public List<CustomerFollowerVo> selectFollowersByExtUserId(String extUserId) {
        if (StrUtils.isNotEmpty(extUserId)) {
            return extFollowerMapper.selectFollowers(extUserId);
        }
        return Collections.emptyList();
    }

    @Override
    public List<ExtFollowContact> selectFollower(String extUserId, String userId) {
        QueryWrapper<ExtFollowContact> select = new QueryWrapper<>();
        if (StrUtils.isNotEmpty(extUserId)) {
            select.eq("ext_user_id", extUserId);
        }
        if (StrUtils.isNotEmpty(userId)) {
            select.eq("user_id", userId);
        }
        return extFollowerMapper.selectList(select);
    }
}
